<?php

// +----------------------------------------------------------------------
// | ThinkAdmin
// +----------------------------------------------------------------------
// | 版权所有 2014~2020 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
// +----------------------------------------------------------------------
// | 官方网站: https://thinkadmin.top
// +----------------------------------------------------------------------
// | 开源协议 ( https://mit-license.org )
// +----------------------------------------------------------------------
// | gitee 代码仓库：https://gitee.com/zoujingli/ThinkAdmin
// | github 代码仓库：https://github.com/zoujingli/ThinkAdmin
// +----------------------------------------------------------------------

namespace app\wechat\controller;

//use controller\BasicAdmin;
//use service\DataService;
//use service\ToolsService;
//use think\Db;
//use think\exception\HttpResponseException;
//use app\data\service\GoodsService;
//use think\admin\extend\CodeExtend;

use think\admin\Controller;
use think\facade\Request;
use think\facade\Db;

/**
 * 小程序菜单
 * Class Meal
 * @package app\meal\controller
 */
class MenuPicture extends Controller
{

    private $table = 'wecha_menu_picture';

    /**
     * 小程序菜单管理
     * @auth true
     * @menu true
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */

    public function index()
    {
        $this->title = '菜单图片';
        $this->_query($this->table)->where(['is_deleted'=>1])->order('id desc')->page();
    }
    /**
     * 小程序菜单添加
     * @auth true
     * @menu true
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function add()
    {
        if ($this->request->isPost()) {
            $post=$this->request->post();

            if ($post['data'][0]['mini_appid'] && $post['data'][0]['imgurl'] === '#') {
                $post['data'][0]['imgurl'] = '';
            }
            $data['img']=$post['data'][0]['img'];
            $data['imgurl']=$post['data'][0]['imgurl'];
            $data['menuname']=$post['data'][0]['menuname'];
            $data['mini_appid']=$post['data'][0]['mini_appid'];
            Db::name('wecha_menu_picture')->insert($data);
        }

        return $this->_form($this->table, 'form');
    }
    /**
     * 小程序菜单编辑
     * @auth true
     * @menu true
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function edit()
    {
        if ($this->request->isPost()) {
            $id = $this->request->get('id');
            $post=$this->request->post();

            if ($post['data'][0]['mini_appid'] && $post['data'][0]['imgurl'] === '#') {
                $post['data'][0]['imgurl'] = '';
            }
            $data['img']=$post['data'][0]['img'];
            $data['imgurl']=$post['data'][0]['imgurl'];
            $data['menuname']=$post['data'][0]['menuname'];
            $data['mini_appid']=$post['data'][0]['mini_appid'];
            Db::name('wecha_menu_picture')->where('id',$id)->update($data);
        }
        return $this->_form($this->table, 'form');;
    }

    /**
     * 修改状态
     * @auth true
     * @throws \think\db\exception\DbException
     */
    public function state()
    {
        $this->_save($this->table);
    }

    /**
     * 删除
     * @auth true
     * @throws \think\db\exception\DbException
     */
    public function del()
    {
        $id = $this->request->get('id');
        Db::name('wecha_menu_picture')->where('id',$id)->update(['is_deleted'=>0]);
        return $this->_form($this->table, 'index');;
    }
}
